package org.txazo.blog.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.txazo.blog.util.HttpUtil;

/**
 * 静态资源过滤器
 * 
 * @author Tu XiaoZhou
 * @since 2013-09-04
 * 
 */
public class StaticResourceFilter implements Filter {

	private static final String FAVICON_ICO = "/favicon.ico";
	private static final String EXPIRES = "Expires";

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		String uri = request.getRequestURI();

		if (HttpUtil.isStaticResourceOfURI(uri) || uri.equals(FAVICON_ICO)) {
			response.setHeader(EXPIRES, HttpUtil.getExpireTime());
		}

		chain.doFilter(req, resp);
	}

	@Override
	public void destroy() {
	}

}
